package com.ahao.servlet.grade;

import com.ahao.model.GradeDTO;
import com.ahao.model.RespBean;
import com.ahao.service.GradeService;
import com.ahao.utils.CommonsUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
    GradeService gradeService = new GradeService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        if ("page".equals(action)) {
            req.getRequestDispatcher("/WEB-INF/jsp/grade/gradeList.jsp").forward(req, resp);
        } else if ("data".equals(action)) {
            List<GradeDTO> list = gradeService.getAllGrades();
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(list));
        }else if ("data_from_clazz".equals(action)) {
            //获取 json
            //查询所有的年级
            List<GradeDTO> list = gradeService.getAllGrades();
            GradeDTO gradeDTO = new GradeDTO();
            gradeDTO.setGid(-1);
            gradeDTO.setGradeName("全部");
            list.add(0, gradeDTO);
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().write(new ObjectMapper().writeValueAsString(list));
        }
    }

    /**
     * 添加年级和课程
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gradeName = req.getParameter("gradeName");
        String[] coursesNames = req.getParameterValues("cid");
        List<Integer> list = Arrays.stream(coursesNames).map(c -> Integer.parseInt(c)).collect(Collectors.toList());
        Integer i = gradeService.addGrade(gradeName, list);

        resp.setContentType("application/json;charset=utf-8");
        RespBean respBean = null;
        if(i == CommonsUtils.UPDATE_SUCCESS){
            respBean = RespBean.ok("添加成功!");
        }else if (i == CommonsUtils.TABLE_DATA_REPEAT){
            respBean = RespBean.error("年级名已存在，添加失败!");
        }else if(i == CommonsUtils.OTHER_EXCEPTION){
            respBean = RespBean.error("服务器异常！添加失败!");
        }else {
            respBean = RespBean.error("服务器异常！添加失败!");
        }

        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }

    /**
     * 删除年级和课程
     */
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gid = req.getParameter("gid");
        Integer i = gradeService.deleteGradeByGid(Integer.parseInt(gid));

        resp.setContentType("application/json;charset=utf-8");
        RespBean respBean = null;
        if(i == CommonsUtils.UPDATE_SUCCESS){
            respBean = RespBean.ok("删除成功!");
        }else if (i == CommonsUtils.OTHER_EXCEPTION){
            respBean = RespBean.error("删除失败!");
        }else{
            respBean = RespBean.error("删除失败!");
        }

        resp.getWriter().write(new ObjectMapper().writeValueAsString(respBean));
    }
}
